package esn.temp;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;

import esn.core.ejb.entity.*;
import esn.core.ejb.session.*;

public class TempTest {
	
	private static Hashtable <String,Object> lookupCache = new Hashtable<String,Object>();
	private static InitialContext initCtx;
	private static final String JB_APPLIANCE_IP = "192.168.9.129";
	
	private static void obtainInitialContext() throws Exception {
        Hashtable<String,String> props = new Hashtable<String,String>();
        props.put(
            Context.INITIAL_CONTEXT_FACTORY,
            "org.jnp.interfaces.NamingContextFactory");
        props.put(
            Context.URL_PKG_PREFIXES,
            "org.jboss.naming:org.jnp.interfaces");
        props.put(Context.PROVIDER_URL, "jnp://" + JB_APPLIANCE_IP + ":1099");
        initCtx = new InitialContext(props);
    }
	
	public static Object lookup(String name, boolean ignoreCache)
		throws Exception {
		if(initCtx == null) obtainInitialContext();
		
		if(lookupCache.containsKey(name)==false || ignoreCache==true) {
			Object ref = initCtx.lookup(name);
			if(ref!=null) lookupCache.put(name,ref);
		}
		
		return lookupCache.get(name);
	}

	public static void main(String[] args) throws Exception {
		Object ref = lookup("esn-core/ESNProfileManager/remote",true);
		ESNProfileManagerRemote agentObject = (ESNProfileManagerRemote)
			PortableRemoteObject.narrow(ref,ESNProfileManagerRemote.class);
		System.out.println("Obtained reference to agent");
		
		UserProfile profile = new UserProfile();
		UserAuthentication auth = new UserAuthentication();
		auth.setUserId("test1");
		auth.digestPassword("password4test1");
		UserRole role = new UserRole();
		role.setRole(UserRole.ROLE__ESN_USER);
		role.setUserAuthentication(auth);
		auth.getRoles().add(role);
		profile.setAuthentication(auth);
		
		agentObject.createProfile(profile);
	}

}
